package com.dk.log;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect //声明是一个切面类 aop
public class LogAspect {


    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

   /**使用Aop切点表达式  自定义注解来解决 */
    @Pointcut("execution(public * com.dk.controller.*.*(..))")
    public void aopLog() {
    }


    //aop通知 前置通知 后置通知 异常通知 返回后通知 环绕通知

    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint) {
        //打印进入哪个方法 参数是什么
        Object[] args = joinPoint.getArgs();
        //获取方法
        Signature signature = joinPoint.getSignature();
        logger.info("进入方法：{}，参数为：{}", signature, args);
    }


    //返回后通知
    @AfterReturning (pointcut = "aopLog()",returning = "obj")
    public void  doAfterReturning (Object obj )throws Throwable{
        logger.info("方法执行成功。返回值为：{}", JSONObject.toJSONString(obj));
    }
}
